﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using YJKRevitBase.WinForm;

namespace YJKExcelFormTool
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdDrawDoorWindowLegend : YJKCommandManual
	{
		[DllImport("user32.dll")]
		private static extern void BlockInput(bool Block);

		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Result result2;
			try
			{
				if (CmdDrawDoorWindowLegend._hWndRevit == null)
				{
                    CmdDrawDoorWindowLegend._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				cmdData.Application.Application.FailuresProcessing += this.OnFailuresProcessing;
				Result result = TableInterface.DrawDoorWindowLegend(cmdData, CmdDrawDoorWindowLegend._hWndRevit, ref message);
				CmdDrawDoorWindowLegend.BlockInput(false);
				result2 = result;
			}
			catch
			{
				CmdDrawDoorWindowLegend.BlockInput(false);
				result2 = Autodesk.Revit.UI.Result.Failed;
			}
			return result2;
		}

		private void OnFailuresProcessing(object sender, FailuresProcessingEventArgs e)
		{
			FailuresAccessor failuresAccessor = e.GetFailuresAccessor();
			string transactionName = failuresAccessor.GetTransactionName();
			IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages();
			if (failureMessages.Count == 0)
			{
				e.SetProcessingResult((FailureProcessingResult)0);
				return;
			}
			if (transactionName.Equals("create legend"))
			{
				foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
				{
					failuresAccessor.DeleteWarning(failureMessageAccessor);
				}
				e.SetProcessingResult((FailureProcessingResult)1);
				return;
			}
			e.SetProcessingResult((FailureProcessingResult)0);
		}

        public static WindowHandle _hWndRevit;
	}
}
